#!/usr/bin/env python3
import os
from pathlib import Path

def replace_line_in_file(line_number, new_content):

    file = open("./stat.max", 'r')

    lines = file.readlines()

    file.close()

    lines[line_number] = new_content


    file = open("./stat.max", 'w')

    file.writelines(lines)

    file.close()


def get_max(thds, score, sys_value, sys_path):

    data_max_get = open("./stat.max", 'r+')
    line = -1
    for _l in data_max_get.readlines():
        _ll = _l.strip()
        line += 1
        # skip null lines
        if not _ll:
            continue

        # skip comment lines
        if _ll.find("#") == 0:
            continue

        #sys value
        tmpidx = _ll.find("value:")
        if tmpidx == -1:
            continue
        tmp_value = _ll[tmpidx + 6:].strip();
        #if int(tmp_value) != sys_value:
        #    continue

        # thds
        tmpidx = _ll.find("thds:")
        if tmpidx == -1:
            continue
        eidx = _ll.find("paremater:")
        if eidx == -1:
            continue
        tmp_thds = _ll[tmpidx + 5:eidx - 1].strip()
        if int(tmp_thds) != thds:
            continue

        qpsidx = _ll.find("qps:")
        if qpsidx == -1:
            continue
        old_max = _ll[qpsidx + 4: tmpidx - 1].strip()
        if float(old_max) >= score:
            return
        print("--------+++++++++", str(score))
        new_content = "result qps:" + str(score) + " thds:" + str(thds) + " paremater:" + sys_path + "\n"
        data_max_get.close()
        print("--------+++++++++", new_content)
        replace_line_in_file(line, new_content)
        return
    data_max_get.close()

    data_max = open("./stat.max", 'a')
    new_content = "\nresult qps:" + str(score) + " thds:" + str(thds) + " paremater:" + sys_path + "\n"
    data_max.write(new_content)
    data_max.close()
 
    return score

def stat_file(_f):
    data_out = open("./stat.file", 'w+') 
    data_max_tmp = open("./stat.max.tmp", 'w+')
    sys_path = ""
    sys_value = ""
    threads = ""
    for root, dirs, files in os.walk("./history", topdown=True):
        for name in files:
            fname = os.path.join(root, name)
            if name.endswith(".swp"):
                continue
            with open(fname, 'r') as _fd:
                for _l in _fd.readlines():
                    _ll = _l.strip()

                    # skip null lines
                    if not _ll:
                        continue

                    # skip comment lines
                    if _ll.find("#") == 0:
                        continue

                    # skip other files
                    if _ll.find(_f) == 0:
                        continue

                    tmpidx = _ll.find("queries")
                    if tmpidx != -1:
                        tmpidx = _ll.find("(")
                        if tmpidx == -1:
                            continue
                        eidx = _ll.find("per sec")
                        if tmpidx == -1:
                            continue
                        
                        key = _ll[tmpidx + 1 : eidx].strip()
                        print("result qps:" + key + " thds:" + threads + " paremater:" + sys_path + "\n", file=data_out, end=' ', flush=True)
                        tmp_result = get_max(int(threads), float(key), int(sys_value), sys_path)
                        #print("result qps:" + str(tmp_result) + " thds:" + threads + " paremater:" + sys_path + "\n", file=data_max_tmp, end=' ', flush=True)
                        continue 

                    tmpidx = _ll.find("file:")
                    if tmpidx != -1:
                        sys_path = _ll[tmpidx:].strip()
                        tmpidx = _ll.find("value:")
                        if tmpidx != -1:
                            sys_value = _ll[tmpidx + 6:].strip();
                        continue

                    tmpidx = _ll.find("thds:")
                    if tmpidx == -1:
                        continue
                    eidx = _ll.find("tps:")
                    threads = _ll[tmpidx + 5:eidx].strip()
    
    data_max_tmp.close()
    data_out.close()

def do_the_stat():
    file = open("./stat.file", 'a')
    file.close()

    file = open("./stat.max", 'a')
    file.close()

    stat_file("mysql")

    #os.unlink()
